package Test;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import Container.Bid;
import Container.Center;
import Container.Criteria;
import Container.Item;
import Container.Login;
import Container.Message;
import Container.Transaction;
import Container.User;
import Container.UserInfo;
import DBControl.SimpleJDBC;
import DBControl.DBInterface;

public class TestSet {
	DBInterface s = null;
	
	public static void main(String[] args) throws SQLException {
		TestSet ts = new TestSet();
		ts.testCase1();
		ts.testCase3();
		ts.testCase5();
		ts.testCase6();
		ts.testCase9();
		ts.testCase10();
	}

	public TestSet() {
		s = new SimpleJDBC();
	}
	
	//User Register
	private void testCase1() throws SQLException {
		User user = new User("Lily", "111");
		UserInfo info = new UserInfo("Lily J", "2261111111", "Waterloo", "Lily@uw.ca");
		int uid = s.register(user, info);
		
		User user1 = new User("Tom", "111");
		UserInfo info1 = new UserInfo("Tom T", "2262222222", "Waterloo", "lily@uw.ca");
		int uid1 = s.register(user1, info1);
		
		
		User user2 = new User("Lucy", "111");
		UserInfo info2 = new UserInfo("Lucy L", "2263333333", "Waterloo", "lucy@uw.ca");
		int uid2 = s.register(user2, info2);
		
		
		User user3 = new User("Lee", "111");
		UserInfo info3 = new UserInfo("Lee J", "2264444444", "Waterloo", "lee@uw.ca");
		int uid3 = s.register(user3, info3);
		
		
		User user5 = new User("Mary", "111");
		UserInfo info5 = new UserInfo("Mary M", "2265555555", "Waterloo", "mary@uw.ca");
		int uid5 = s.register(user5, info5);
		
		System.out.println("Uid: " + uid);
	}
	
	//User Login
	private void testCase2() throws SQLException {
		User user = new User("test5", "test4");
		
		Login login = s.login(user);
		
		if (login == null) {
			System.out.println("Error");
		} 
		else {
			if (login.getUid() == -1) {
				System.out.println("Wrong username or password");
			}
			else {
				System.out.println("Success!\nuid: " + login.getUid());
				System.out.println("Name: " + login.getName());
			}
		}
	}
	
	//Add Category
	private void testCase3() throws SQLException {
		s.addCategory("Fashion");
		s.addCategory("Electronics");
		s.addCategory("Arts");
		s.addCategory("Sporting Goods");
		s.addCategory("Toys & Hobbies");
		s.addCategory("Others");
	}
	
	//Retrieve Category
	private void testCase4() throws SQLException {
		HashMap<String, Integer> result = s.getCategory();
		
		Iterator<String> it = result.keySet().iterator();
		
		while(it.hasNext()) {
			System.out.println(it.next());
		}
	}
	
	//Add Item
	private void testCase5() throws SQLException {
		//1
		s.addItem(new Item("t-shirt", null, "red  cloth", 1, 20, 300, 10 ,1));
		s.addItem(new Item("t-shirt1", "101.jpg", "nice cloth", 1, 2100, 30000, 10 ,1));
		s.addItem(new Item("t-shirt2", "102.jpg", "good cloth", 2, 2200, 30000, 10 ,1));
		s.addItem(new Item("t-shirt3", "103.jpg", "nice cloth", 2, 2300, 30000, 10 ,1));
		s.addItem(new Item("t-shirt4", "104.jpg", "blue cloth", 2, 2400, 30000, 10 ,1));
		s.addItem(new Item("t-shirt5", "105.jpg", "green clth", 3, 9500, 30000, 10 ,1));
		s.addItem(new Item("t-shirt6", "106.jpg", "nice cloth", 3, 9600, 30000, 20 ,1));
		s.addItem(new Item("t-shirt7", "107.jpg", "nice cloth", 3, 9700, 30000, 20 ,1));
		s.addItem(new Item("t-shirt8", "108.jpg", "nice cloth", 3, 9800, 30000, 20 ,1));
		s.addItem(new Item("t-shirt9", "109.jpg", "nice cloth", 3, 9900, 30000, 20 ,1));
		s.addItem(new Item("t-shirt10", "110.jpg", "nice cloth", 3, 9000, 30000, 20 ,1));
		s.addItem(new Item("t-shirt11", "111.jpg", "nice cloth", 4, 9100, 30000, 30 ,1));
		s.addItem(new Item("t-shirt12", "112.jpg", "nice cloth", 4, 9200, 30000, 30 ,1));
		s.addItem(new Item("t-shirt13", "113.jpg", "nice cloth", 4, 9300, 30000, 30 ,1));
		s.addItem(new Item("laptop ", "pic.jpg", "very good laptop", 1, 2000, 30000, 10 ,2));
		
		//16
		s.addItem(new Item("laptop ", "pic1.jpg", "laptop", 1, 2100, 30000, 10 ,2));
		s.addItem(new Item("laptop ", "pic2.jpg", "laptop", 2, 2200, 30000, 10 ,2));
		s.addItem(new Item("pad", "pic3.jpg", "pad", 2, 2300, 20000, 10 ,2));
		s.addItem(new Item("HDD", "pic4.jpg", "Good HDD", 2, 2400, 30000, 10 ,2));
		s.addItem(new Item("key board", "pic5.jpg", "key board", 3, 9500, 30000, 10 ,2));
		
		//21
		s.addItem(new Item("mouse", "pic6.jpg", "mouse", 3, 9600, 30000, 20 ,2));
		s.addItem(new Item("monitor", "pic7.jpg", "monitor", 5, 9700, 0, 20 ,2));
		s.addItem(new Item("tennis bat", "spt1.jpg", "one pair", 1, 2000, 20000, 10 ,4));
		s.addItem(new Item("bedminton bat", "spt2.jpg", "brand new ", 1, 2100, 20000, 10 ,4));
		s.addItem(new Item("basketball", "spt3.jpg", "brand new ", 2, 2200, 20000, 10 ,4));
		
		//26
		s.addItem(new Item("football", "spt4.jpg", "brand new ", 2, 2300, 20000, 10 ,4));
		s.addItem(new Item("racket", "spt5.jpg", "brand new ", 2, 2400, 20000, 10 ,4));
		s.addItem(new Item("cannoe", "spt6.jpg", "brand new ", 3, 9500, 20000, 10 ,4));
		s.addItem(new Item("voleyball", "spt7.jpg", "brand new ", 3, 9600, 20000, 20 ,4));
		s.addItem(new Item("swimming goggle", "spt8.jpg", "", 3, 9700, 20000, 20 ,4));
		
		//31
		s.addItem(new Item("shoes", "1001.jpg", "brand new shoes", 3, 9800, 20000, 20 ,4));
		s.addItem(new Item("shoes", "1002.jpg", "used shoes", 3, 2000, 10000, 20 ,4));
		s.addItem(new Item("shoes", "1003.jpg", "new shoes", 3, 8000, 20000, 20 ,4));
		s.addItem(new Item("shoes", "1004.jpg", "good shoes", 4, 8100, 20000, 30 ,4));
		s.addItem(new Item("shoes", "1005.jpg", "very good shoes", 4, 8200, 20000, 30 ,4));
		
		//36
		s.addItem(new Item("shoes", "1006.jpg", "shoes", 4, 8300, 20000, 30 ,4));
		s.addItem(new Item("shoes", "1007.jpg", "shoes", 3, 8600, 20000, 20 ,4));
		s.addItem(new Item("angry birds", "images (1).jpg", "good toy", 5, 2000, 70000, 10 ,5));
		s.addItem(new Item("toy1", "images (2).jpg", "good toy", 5, 2100, 90000, 10 ,5));
		s.addItem(new Item("toy2", "images (3).jpg", "good toy", 2, 2200, 20000, 10 ,5));
		
		
		//41
		s.addItem(new Item("school bus", "images (4).jpg", "good toy", 2, 2300, 30000, 10 ,5));
		s.addItem(new Item("xylophone", "images (5).jpg", "good toy", 2, 2400, 40000, 10 ,5));
		s.addItem(new Item("truck", "images (6).jpg", "good toy", 3, 9500, 50000, 10 ,5));
		s.addItem(new Item("angry bird", "images (7).jpg", "good toy", 3, 9600, 60300, 20 ,5));
		s.addItem(new Item("toy7", "images (8).jpg", "good toy", 3, 9700, 30000, 20 ,5));
		
		//46
		s.addItem(new Item("duck", "images (9).jpg", "good toy", 3, 9800, 20000, 20 ,5));
		s.addItem(new Item("hello kitty", "images (10).jpg", "great toy", 3, 9900, 20000, 20 ,5));
		s.addItem(new Item("lego", "images (11).jpg", "great toy", 3, 9000, 20000, 20 ,5));
		s.addItem(new Item("lion", "images (12).jpg", "great toy", 4, 9100, 20000, 30 ,5));
		s.addItem(new Item("cube", "images.jpg", "great toy", 4, 9300, 20000, 30 ,5));
	}
	
	//Add comment
	private void testCase6() throws SQLException {
		s.comment(4, 2, "Nice!");
		s.comment(2, 2, "Nice!");
		s.comment(3, 4, "Nice!");
		s.comment(4, 2, "Nice!");
		s.comment(5, 7, "Nice!");
		s.comment(4, 2, "Nice!");
		s.comment(4, 9, "Nice!");
		
		//s.comment(4, 8, "Great!");
	}
	
	//Get item detail
	private void testCase7() throws SQLException {
		Item item = s.viewDetail(1);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
		
		System.out.println("iid: " + item.getIid());
		System.out.println("name: " + item.getName());
		System.out.println("category: " + item.getCategoryname());
		System.out.println("description: " + item.getDescription());
		System.out.println("seller id: " + item.getSeller());
		System.out.println("bidder id: " + item.getBidder());
		System.out.println("co: " + item.getCo());
		System.out.println("bo: " + item.getBo());
		System.out.println("Start Date: " + format.format(item.getStart()));
		System.out.println("End Date: " + format.format(item.getEnd()));
		System.out.println("\nComments: ");
		
		ArrayList<Message> comment = item.getComment();
		if (comment != null) {
			for (int i = 0; i < comment.size(); i++) {
				System.out.println("User name: " + comment.get(i).getUsername());
				System.out.println("Date: " + format.format(comment.get(i).getDate()));
				System.out.println("Comment: " + comment.get(i).getComment());
				System.out.println();
			}
		}
	}
	
	//Search
	private void testCase8() throws SQLException {
		Criteria cri = new Criteria();
		cri.setName("more");
		cri.setCategory(1);
		cri.setMin(50);
		cri.setMax(90);
		
		ArrayList<Item> result = s.search(cri);
		Item item = null;
		
		if (result != null) {
			for (int i = 0; i < result.size(); i++) {
				item = result.get(i);
				System.out.println("name: " + item.getName());
				System.out.println("co: " + item.getCo());
				System.out.println("bo: " + item.getBo());
				System.out.println();
			}
		}
		else {
			System.out.println("No result!");
		}
	}
	
	//Bid
	private void testCase9() throws SQLException {
//		System.out.println("Result: " + s.bidItem(10, 3, 250));
		s.bidItem(5, 5, 100);
		s.bidItem(4, 28, 150);
		s.bidItem(1, 9, 120);
		s.bidItem(2, 15, 40);
		s.bidItem(3, 18, 60);
		s.bidItem(5, 34, 100);
		s.bidItem(1, 10, 135);
		s.bidItem(2, 14, 200);
		s.bidItem(4, 21, 180);
		s.bidItem(3, 49, 270);
	}
	
	//Buyout
	private void testCase10() throws SQLException {
		//System.out.println("Result: " + s.buyout(10, 10));
		s.buyout(3, 1);
		s.buyout(4, 3);
		s.buyout(1, 6);
		s.buyout(5, 15);
		s.buyout(5, 34);
		s.buyout(1, 17);
		s.buyout(4, 21);
	}
	
	//Check Completion
	private void testCase11() throws SQLException {
		System.out.println("Result: " + s.checkCompletion());
	}
	
	//View Center
	private void testCase12() throws SQLException {
		Center center = s.viewCenter(3);
		
		UserInfo info = center.getUserinfo();
		System.out.println("User Info:\nName: " + info.getName());
		System.out.println("Telephone: " + info.getTel());
		System.out.println("Address: " + info.getAddress());
		System.out.println("E-mail: " + info.getEmail());
		
		System.out.println("Selling Items");
		ArrayList<Item> items = center.getItems();
		for (int i = 0; i < items.size(); i++) {
			Item item = items.get(i);
			System.out.print("iid: " + item.getIid());
			System.out.println("    Item Name: " + item.getName());
			System.out.println();
		}
		
		System.out.println("Bidding Items");
		ArrayList<Bid> bids = center.getBids();
		for (int i = 0; i < bids.size(); i++) {
			Bid bid = bids.get(i);
			System.out.print("Item Name: " + bid.getItemname());
			System.out.println("    Amount: " + bid.getAmount());
			System.out.println("    Date: " + bid.getDate().toString());
			System.out.println();
		}
		
		System.out.println("Won Items");
		ArrayList<Transaction> buys = center.getTrans_Buy();
		for (int i = 0; i < buys.size(); i++) {
			Transaction transbuy = buys.get(i);
			System.out.print("Item Name: " + transbuy.getItemname());
			System.out.println("    Seller Name: " + transbuy.getSellername());
			System.out.println("    Price: " + transbuy.getAmount());
			System.out.println("    Date: " + transbuy.getDate().toString());
			System.out.println();
		}
		
		System.out.println("Sold Items");
		ArrayList<Transaction> sells = center.getTrans_Sell();
		for (int i = 0; i < sells.size(); i++) {
			Transaction transsell = sells.get(i);
			System.out.print("Item Name: " + transsell.getItemname());
			System.out.println("    Price: " + transsell.getAmount());
			System.out.println("    Date: " + transsell.getDate().toString());
			System.out.println();
		}
	}
}
